IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'MD3.f_SchemaExists'))
   DROP FUNCTION MD3.f_SchemaExists
GO

-- Function to determine if the given schema exists.

CREATE FUNCTION MD3.f_SchemaExists (@SchemaName SYSNAME)
    RETURNS BIT
    WITH RETURNS NULL ON NULL INPUT 
AS

BEGIN

    -- Remove any brackets.
    SELECT @SchemaName=REPLACE(@SchemaName,'[','')  
    SELECT @SchemaName=REPLACE(@SchemaName,']','')  

    -- Check to see if the schema exists.
    IF EXISTS 
        (SELECT * FROM sys.schemas WHERE schema_id = schema_id(@SchemaName))
        RETURN 1
    ELSE
        RETURN 0

RETURN 0
END
GO
